/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jeff.studyclient.ui.renderer;

import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

/**
 *
 * @author zhou.zhefu
 */
public class TextBox extends JTextField {
    public TextBox() {
		this(0);
	}

	public TextBox(int limit) {
		init(limit);
	}

	public TextBox(String text) {
		this(text, 0);
	}

	public TextBox(String text, int limit) {
		super(text);
		init(limit);
	}

	private void init(int limit) {
		((AbstractDocument) getDocument()).setDocumentFilter(new FixedSizeFilter(limit));
	}
    
    /**
	 * FixedSizeFilter class.
	 */
	public static class FixedSizeFilter extends DocumentFilter {

		private int maxSize;

		/**
		 * Constructor.
		 * Limit is the maximum number of characters allowed.
		 * @param limit limit value.
		 */
		public FixedSizeFilter(int limit) {
			maxSize = limit;
		}

		// This method is called when characters are inserted into the document
		/**
		 * Invoked prior to insertion of text into the
		 * specified Document. Subclasses that want to conditionally allow
		 * insertion should override this and only call supers implementation as
		 * necessary, or call directly into the FilterBypass.
		 *
		 * @param fb FilterBypass that can be used to mutate Document
		 * @param offset  the offset into the document to insert the content >= 0.
		 *    All positions that track change at or after the given location
		 *    will move.
		 * @param string the string to insert
		 * @param attr      the attributes to associate with the inserted
		 *   content.  This may be null if there are no attributes.
		 * @exception BadLocationException  the given insert position is not a
		 *   valid position within the document
		 */
		@Override
		public void insertString(DocumentFilter.FilterBypass fb, int offset, String string,
				AttributeSet attr) throws BadLocationException {
			replace(fb, offset, 0, string, attr);
		}

		// This method is called when characters in the document are replace with other characters
		/**
		 * Invoked prior to replacing a region of text in the
		 * specified Document. Subclasses that want to conditionally allow
		 * replace should override this and only call supers implementation as
		 * necessary, or call directly into the FilterBypass.
		 *
		 * @param fb FilterBypass that can be used to mutate Document
		 * @param offset Location in Document
		 * @param length Length of text to delete
		 * @param text Text to insert, null indicates no text to insert
		 * @param attrs AttributeSet indicating attributes of inserted text,
		 *              null is legal.
		 * @exception BadLocationException  the given insert position is not a
		 *   valid position within the document
		 */
		@Override
		public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
				String text, AttributeSet attrs) throws BadLocationException {
			int newLength = fb.getDocument().getLength() - length + text.length();
			if (maxSize > 0 && newLength <= maxSize) {
				fb.replace(offset, length, text, attrs);
			}
		}
	}
}
